package com.example.camunda;

import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.dmn.engine.DmnDecisionTableResult;
import org.camunda.bpm.engine.DecisionService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.variable.VariableMap;
import org.camunda.bpm.engine.variable.Variables;
import org.camunda.bpm.spring.boot.starter.annotation.EnableProcessApplication;
import org.camunda.bpm.spring.boot.starter.event.PostDeployEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.event.EventListener;

import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
@EnableProcessApplication
@Slf4j
public class SpringbootCamundaApplication {
    @Autowired
    private RuntimeService runtimeService;

    public static void main(String[] args) {
        SpringApplication.run(SpringbootCamundaApplication.class, args);
    }

//    @EventListener
//    public void autoDecide(PostDeployEvent event) {
//        log.info(">>>>>>>>>>>> 启动了一个决策实例 <<<<<<<<<<<<<<<");
//        // 创建引擎
//        ProcessEngine processEngine = event.getProcessEngine();
//        // 用引擎创建决策
//        DecisionService decisionService = processEngine.getDecisionService();
//        // 设定当前流程的决策值
//        VariableMap variableMap = Variables.createVariables()
//                .putValue("item", "小米11");
//        // 决策
//        DmnDecisionTableResult dmnDecisionRuleResults = decisionService.evaluateDecisionTableByKey("approve-payment", variableMap);
//        // 获取决策结果，返回类型以modeler设计为准
//        boolean decisionResult = dmnDecisionRuleResults.getSingleEntry();
//        log.info("决策结果：{}", decisionResult);
//    }
//
    @EventListener
    public void startProcess(PostDeployEvent event){
        log.info(">>>>>>>>>>>> 启动了一个流程实例 <<<<<<<<<<<<<<<");
        // 启动一个流程实例
        Map<String, Object> variables = new HashMap<>(4);
        variables.put("item", "小米");
        variables.put("amount", "2000");
        variables.put("approved", false);
        variables.put("applier", "张三");
        runtimeService.startProcessInstanceByKey("payment-retrieval", variables);
    }

}
